package com.atguigui.leetcode;

/**
 * Project: leetcode
 * Package: com.atguigui.leetcode
 * Version: 1.0
 * <p>
 * Created by  wangjiaxin  on 2021/2/19 11:19 上午
 */
//Java：最大连续1的个数 III
public class P1004MaxConsecutiveOnesIii {
    public static void main(String[] args) {
        Solution solution = new P1004MaxConsecutiveOnesIii().new Solution();
        // TO TEST
    }
    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public int longestOnes(int[] A, int K) {
            int N = A.length;
            int res = 0;
            int left = 0, right = 0;
            int zeros = 0;
            while (right < N) {
                if(A[right] == 0){
                    zeros++;
                }
                //左侧指针一直往右移动，直到遇到0
                while (zeros > K) {
                    if (A[left++] == 0)
                        zeros --;
                }
                res = Math.max(res, right - left + 1);
                right ++;
            }
            return res;
        }
    }
}
